/**
 * 
 */
package top.lcpsky.flashcards.service;

import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Maps.newHashMap;

import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import com.google.common.base.Joiner;

import top.lcpsky.flashcards.entity.Cards;
import top.lcpsky.flashcards.entity.CardsExample;
import top.lcpsky.flashcards.entity.CardsExample.Criteria;
import top.lcpsky.flashcards.entity.CardsWithBLOBs;
import top.lcpsky.flashcards.mapper.CardsMapper;
import top.lcpsky.flashcards.util.ResponseResult;
import top.lcpsky.flashcards.util.excel.ExcelUtil;

/**
 * @ClassName: CardsService
 * @author: lcp
 * @date: 2020年5月9日 下午9:42:45
 * 
 * @Copyright: 2020 www.ankept.com Inc. All rights reserved.
 */
@Service
public class CardsService {
	@Autowired
	private CardsMapper cardsMapper;

	public List<CardsWithBLOBs> findAll(Byte type) {
		CardsExample example = new CardsExample();
		Criteria cardsCriteria = example.createCriteria();
		if (type != null) {
			cardsCriteria.andTypeEqualTo(type);
		}
		return cardsMapper.selectByExampleWithBLOBs(example);
	}

	/**
	 * @Title: findCardById @param: @param id @param: @return @return: Cards @throws
	 */
	public CardsWithBLOBs findCardById(Integer id) {
		CardsExample example = new CardsExample();
		Criteria cardsCriteria = example.createCriteria();
		if (id != null) {
			cardsCriteria.andIdEqualTo(id);
		} else {
			return new CardsWithBLOBs();
		}
		List<CardsWithBLOBs> res = cardsMapper.selectByExampleWithBLOBs(example);
		if (!CollectionUtils.isEmpty(res)) {
			return res.get(0);
		}
		return null;
	}

	/**
	 * @Title: findCardByType @param: @param type @param: @return @return:
	 * Cards @throws
	 */
	public CardsWithBLOBs findCardByType(int type) {
		CardsExample example = new CardsExample();
		Criteria cardsCriteria = example.createCriteria();
		cardsCriteria.andTypeEqualTo(Integer.valueOf(type).byteValue());
		example.setOrderByClause("RAND() desc limit 1");
		List<CardsWithBLOBs> resList = cardsMapper.selectByExampleWithBLOBs(example);
		if (CollectionUtils.isEmpty(resList)) {
			return null;
		}
		return resList.get(0);
	}

	/**
	 * @Title: deleteCardById @param: @param id @return: void @throws
	 */
	public Boolean deleteCardById(Integer id) {
		return cardsMapper.deleteByPrimaryKey(id) > 0 ? true : false;
	}

	/**
	 * @Title: updateCard @param: @param card @return: void @throws
	 */
	public void updateCard(CardsWithBLOBs card) {
		cardsMapper.updateByPrimaryKeySelective(card);
	}

	/**
	 * @Title: findAllByKnown @param: @param i @param: @return @return:
	 * List<CardsWithBLOBs> @throws
	 */
	public List<CardsWithBLOBs> findAllByKnown(Byte known) {
		CardsExample example = new CardsExample();
		Criteria cardsCriteria = example.createCriteria();
		if (known != null) {
			cardsCriteria.andKnownEqualTo(known);
		}
		return cardsMapper.selectByExampleWithBLOBs(example);
	}

	/**
	 * @Title: addCard @param: @param card @return: void @throws
	 */
	public void addCard(CardsWithBLOBs card) {
		card.setKnown((byte) 0);
		cardsMapper.insert(card);
	}

	public void updateCardKnown(Integer id) {
		Cards card = cardsMapper.selectByPrimaryKey(id);
		card.setKnown((byte) 1);
		cardsMapper.updateByPrimaryKey(card);
	}

	public void updateCardUnknown(Integer id) {
		Cards card = cardsMapper.selectByPrimaryKey(id);
		card.setKnown((byte) 0);
		cardsMapper.updateByPrimaryKey(card);
	}

	/**   
	 * @Title: importInfo   
	 * @param: @param in
	 * @param: @param file
	 * @param: @param month
	 * @param: @return      
	 * @return: ResponseResult      
	 * @throws   
	 */
	public ResponseResult importInfo(InputStream in, MultipartFile file, String month) {
		ResponseResult res =  new ResponseResult(200, "导入成功");
		try {
			List<List<Object>> listob = ExcelUtil.getBankListByExcel(in, file.getOriginalFilename());
			if (!CollectionUtils.isEmpty(listob)) {
					insertCardsBatch(listob);
			}
		}catch (Exception e) {
			res = new ResponseResult(200, e.getMessage());
		}
		return res;
	}

	/**   
	 * @Title: insertCardsBatch   
	 * @param: @param listob      
	 * @return: void      
	 * @throws   
	 */
	private void insertCardsBatch(List<List<Object>> listob) {
		List<Map<String, String>> listMapob = newArrayList();
		for (List<Object> list : listob) {
			if (!CollectionUtils.isEmpty(list) && StringUtils.hasText(String.valueOf(list.get(0)))) {
				Map<String, String> map = newHashMap();
				map.put("type", String.valueOf(list.get(0)));
				map.put("front",  String.valueOf(list.get(1)));
				map.put("back",  String.valueOf(list.get(2)));
				listMapob.add(map);
			}else {
				break;
			}
		}
		cardsMapper.insertBatch(listMapob );
	}

}
